<template>
  <div class="home">
    <div class="home-header">
      <div class="home-left">
        <div class="home-time">
          {{ time }}
        </div>
        <div class="user-name">{{ nickName }}</div>
      </div>
      <form action="/">
        <van-search
          v-model="value"
          placeholder="输入商品名称"
          @click="onSearch"
        />
      </form>
    </div>
    <div class="Carousel">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item
          v-for="(item, index) in bannerData"
          :key="index"
          @click="goDetail(item.pid)"
        >
          <img :src="item.bannerImg" alt="" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <div class="home-hot">
      <div class="hot-title">热卖推荐</div>
      <div class="hot-pro">
        <div
          class="hot-pro-item"
          v-for="(item, index) in hotproitem"
          :key="index"
          @click="goDetail(item.pid)"
        >
          <ProItem :product="item" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import ProItem from "../components/ProItem.vue";
export default {
  name: "Home",
  components: {
    ProItem,
  },
  data() {
    return {
      value: "",
      nickName: "",
      hotproitem: [],
      getmyData: [],
      images: "",
      bannerData: [],
      imagess: [],
    };
  },
  created() {
    this.getHotPro();
    this.getMyData();
    this.getBanner();
  },
  methods: {
    getMyData() {
      this.axios({
        method: "get",
        url: "/findMy",
      })
        .then((res) => {
          if (res.data.code === 700) {
            return this.$router.push({ name: "Login" });
          }
          this.nickName = res.data.result[0].nickName;
        })
        .catch((err) => {});
    },
    //  轮播图
    getBanner() {
      this.axios({
        method: "GET",
        url: "/banner",
      })
        .then((res) => {
          this.bannerData = res.data.result;
          if (res.data.code == 700) {
            this.$router.push({ name: Login });
          }
        })
        .catch((err) => {});
    },
    // 获取热卖商品
    getHotPro() {
      let params = {
        key: "type",
        value: "coffee",
      };
      this.axios({
        method: "GET",
        url: "/typeProducts",
        params,
      })
        .then((res) => {
          if (res.data.code == 500) {
            this.hotproitem = res.data.result;
          }
        })
        .catch((err) => {});
    },
    onSearch(value) {
      this.$router.push({ name: "Search", params: { value } });
    },
    goDetail(pid) {
      this.$router.push({ name: "Detail", params: { pid } });
    },
  },
  computed: {
    //     早上6-12
    // 下午12-18
    // 晚上18-24
    // 深夜24-06
    // ```
    time() {
      let time = new Date().getHours();
      if (time >= 6 && time < 12) {
        time = "早上好";
      } else if (time >= 12 && time < 18) {
        time = "下午好";
      } else if (time >= 18 && time < 24) {
        time = "晚上好";
      } else if (time >= 24 && time < 6) {
        time = "深夜好";
      }
      return time;
    },
  },
};
</script>

<style lang="less" scoped>
.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20px;
  line-height: 150px;
  text-align: center;
}
.home {
  overflow-y: auto;
}
.home-header {
  display: flex;
  position: relative;
}
.home-left {
  display: flex;
  padding: 16px;

  font-size: 15px;
}
.home-time {
  color: #888;
}
.user-name {
  color: #0c34ba;
  margin: 3px 0 0 10px;
}
.van-search {
  position: absolute;
  right: 0;
}
.van-swipe {
  border-radius: 10px;
  height: 250px;
  background-size: cover;
}
.van-swipe img {
  width: 100%;
  height: 250px;
  position: relative;
}
.hot-title {
  width: 80px;
  height: 28px;
  margin: 20px 0;
  text-align: center;
  line-height: 28px;
  color: rgba(255, 255, 255, 0.8);
  font-size: 11px;
  border-top-right-radius: 15px;
  background-color: #0c34ba;
}

.hot-pro {
  width: 100%;
  padding-bottom: 60px;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
/deep/.auto-img {
  border-radius: 10px;
}
.hot-pro-item {
  width: 50%-4px;
  margin-top: 12px;
}
</style>